package com.n5x.controller;

import com.github.pagehelper.PageHelper;
import com.n5x.common.base.JsonModel;
import com.n5x.common.base.ResponseCode;
import com.n5x.model.goods.*;
import com.n5x.service.IGoodsService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.List;

/**
 * 商品接口Controller
 * 不需要用户token
 */
@Controller("goods")
@RequestMapping("/goods")
public class GoodsController extends BaseController {

    @Autowired
    IGoodsService iGoodsService;

    //******************************************商品信息(GoodsInfo)*******************************************/

    /**
     * 商品详细页面
     * 通过商品ID查询商品
     *
     * @return JSON
     */
    @RequestMapping(value = "/findGoodsById")
    @ResponseBody
    public JsonModel findGoodsById(@RequestParam(value = "id", required = false) String id) {
        try {
            logger.info("[查询商品详细内容]id:{}", new Object[]{id});
            if (StringUtils.isBlank(id)) {
                return getResultError(ResponseCode.Common_ParamError, "缺少参数:ID");
            }
            GoodsInfo goodsInfo = iGoodsService.findGoodsInfo(id);
            if (goodsInfo == null) {
                return getResultError(ResponseCode.Common_DataNotFound, "找不到商品");
            }
            return getResult(goodsInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return getResultError(ResponseCode.Common_Error, e.toString());
        }
    }

    /**
     * 商品列表
     * 通过商品类型查询商品列表
     */
    @RequestMapping(value = "/findGoods")
    @ResponseBody
    public JsonModel findGoodsByType(@RequestParam(value = "id", required = false) String id, @RequestParam(value = "name", required = false) String name, @RequestParam(value = "isSale", required = false) Integer isSale, @RequestParam(value = "group", required = false) Integer group, @RequestParam(value = "type", required = false) String type, @RequestParam(value = "typeName", required = false) String typeName, @RequestParam(value = "pageNum", required = false) Integer pageNum, @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        try {
            logger.info("[查询商品列表]id:{},name:{},isSale:{},group:{},type:{},pageNum:{},pageSize:{}", new Object[]{id, name, isSale, group, type, pageNum, pageSize});
            GoodsInfoExample.Criteria criteria = new GoodsInfoExample().createCriteria();
            if (StringUtils.isNotBlank(id)) {
                criteria.andIdEqualTo(id);
            }
            if (StringUtils.isNotBlank(name)) {
                criteria.andNameLike("%" + typeName + "%");
            }
            if (isSale != null) {
                if (isSale > 0) {
                    criteria.andPreferentialPriceNotEqualTo(new BigDecimal(0));
                } else {
                    criteria.andPreferentialPriceEqualTo(new BigDecimal(0));
                }
            }
            GoodsGroup.GoodsTypeGroupName goodsTypeGroupName = GoodsGroup.GoodsTypeGroupName.getGoodsTypeGroupName(group);
            GoodsGroupExample.Criteria goodsGroupCriteria = null;
            if (goodsTypeGroupName != null) {
                goodsGroupCriteria = new GoodsGroupExample().createCriteria();
                goodsGroupCriteria.andNameEqualTo(goodsTypeGroupName.name());
            }
            GoodsTypeExample.Criteria goodsTypeCriteria = null;
            if (StringUtils.isNotBlank(type)) {
                goodsTypeCriteria = new GoodsTypeExample().createCriteria();
                goodsTypeCriteria.andIdEqualTo(type);
            }
            if (StringUtils.isNotBlank(typeName)) {
                if (goodsGroupCriteria == null) {
                    goodsTypeCriteria = new GoodsTypeExample().createCriteria();
                }
                goodsTypeCriteria.andNameLike("%" + type + "%");
            }
            if (pageNum != null && pageSize != null) {
                PageHelper.startPage(pageNum, pageSize);
            }
            List<GoodsInfo> list = iGoodsService.findGoodsInfo(criteria, goodsGroupCriteria, goodsTypeCriteria);
            if (CollectionUtils.isEmpty(list)) {
                return getResultError(ResponseCode.Common_DataNotFound, "商品列表为空");
            }
            return getResult(list);
        } catch (Exception e) {
            e.printStackTrace();
            return getResultError(ResponseCode.Common_Error, e.toString());
        }
    }

    //******************************************商品类型(GoodsType)*******************************************/

    /**
     * 列出所有商品类型
     *
     * @return JsonModel
     */
    @RequestMapping(value = "/findGoodsTypes")
    @ResponseBody
    public JsonModel findGoodsTypes(@RequestParam(value = "name", required = false) String name) {
        try {
            logger.info("[查询商品类型列表]name:{}", new Object[]{name});
            GoodsTypeExample.Criteria goodsCriteria = new GoodsTypeExample().createCriteria();
            if (StringUtils.isNotBlank(name)) {
                goodsCriteria.andNameLike("%" + name + "%");
            }
            goodsCriteria.andDisableEqualTo(Boolean.FALSE);
            List<GoodsType> list = iGoodsService.findGoodTypes(goodsCriteria);
            if (CollectionUtils.isEmpty(list)) {
                return getResultError(ResponseCode.Common_DataNotFound, "商品类型列表为空");
            }
            return getResult(list);
        } catch (Exception e) {
            e.printStackTrace();
            return getResultError(ResponseCode.Common_Error, e.toString());
        }
    }
}